diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-14 15:30:34 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-14 16:30:04 +0100 |
| commit | 7063b199b4748a9c354ed37e64cdc84c512f2c0c (patch) | |
| tree | 7506c3003c56b49a248e9adb40be610780bb540e /src/pages/projets/[slug].tsx | |
| parent | 85c4c42bd601270d7be0f34a0767a34bb85e29bb (diff) | |
refactor(pages): rewrite helpers to output schema in json-ld format
* make sure url are absolutes
* nest breadcrumb schema in webpage schema
* trim HTML tags from content/description
* use a regular script instead of next/script (with the latter the
schema is not updated on route change)
* place the script in document head
* add keywords, wordCount and readingTime keys in BlogPosting schema
* fix breadcrumbs in search page (without query)
* add tests (a `MatchInlineSnapshot` will be better but Prettier 3 is
not supported yet)
Diffstat (limited to 'src/pages/projets/[slug].tsx')
| -rw-r--r-- | src/pages/projets/[slug].tsx | 50 |
1 files changed, 17 insertions, 33 deletions
diff --git a/src/pages/projets/[slug].tsx b/src/pages/projets/[slug].tsx index 8985f47..1f9723a 100644 --- a/src/pages/projets/[slug].tsx +++ b/src/pages/projets/[slug].tsx @@ -1,10 +1,8 @@ -/* eslint-disable max-statements */ import type { MDXComponents } from 'mdx/types'; import type { GetStaticPaths, GetStaticProps } from 'next'; import dynamic from 'next/dynamic'; import Head from 'next/head'; import NextImage from 'next/image'; -import Script from 'next/script'; import { useMemo, type ComponentType, type FC } from 'react'; import { useIntl } from 'react-intl'; import { @@ -38,9 +36,8 @@ import type { import { CONFIG } from '../../utils/config'; import { capitalize, - getSchemaJson, - getSinglePageSchema, - getWebPageSchema, + getSchemaFrom, + getWebPageGraph, } from '../../utils/helpers'; import { type Messages, @@ -192,27 +189,16 @@ const ProjectPage: NextPageWithLayout<ProjectPageProps> = ({ data }) => { url: `${CONFIG.url}${slug}`, }; - const webpageSchema = getWebPageSchema({ - description: meta.seo.description, - locale: CONFIG.locales.defaultLocale, - slug, - title: meta.seo.title, - updateDate: meta.dates.update, - }); - const articleSchema = getSinglePageSchema({ - cover: `/projects/${id}.jpg`, - dates: meta.dates, - description: intro, - id: 'project', - kind: 'page', - locale: CONFIG.locales.defaultLocale, - slug, - title, - }); - const schemaJsonLd = getSchemaJson([ - webpageSchema, - articleSchema, - breadcrumbSchema, + const jsonLd = getSchemaFrom([ + getWebPageGraph({ + breadcrumb: breadcrumbSchema, + copyrightYear: new Date(meta.dates.publication).getFullYear(), + cover: `/projects/${id}.jpg`, + dates: meta.dates, + description: intro, + slug, + title, + }), ]); const messages = { @@ -256,14 +242,12 @@ const ProjectPage: NextPageWithLayout<ProjectPageProps> = ({ data }) => { <meta property="og:type" content="article" /> <meta property="og:title" content={title} /> <meta property="og:description" content={intro} /> + <script + // eslint-disable-next-line react/no-danger + dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} + type="application/ld+json" + /> </Head> - <Script - // eslint-disable-next-line react/jsx-no-literals -- Id allowed - id="schema-project" - type="application/ld+json" - // eslint-disable-next-line react/no-danger -- Necessary for schema - dangerouslySetInnerHTML={{ __html: JSON.stringify(schemaJsonLd) }} - /> <PageHeader heading={title} intro={intro} |
